/*********************************************************************************
 *Author:         OnClick
 *Version:        1.0
 *UnityVersion:   2018.3.1f1
 *Date:           2019-01-27
 *Description:    IFramework
 *History:        2018.11--
*********************************************************************************/
using System;
using System.Collections.Generic;
namespace IFramework
{
    public class AStarMap2X : IAstarMap<AStarNode2X>
    {
        private List<AStarNode2X> neighborNodes;
        private AStarNode2X[,] map;
        private int mapWith, mapHeight;
        public AStarMap2X(AStarNode2X[,] map, int mapWith, int mapHeight)
        {
            this.map = map;
            this.mapHeight = mapHeight;
            this.mapWith = mapWith;
            neighborNodes = new List<AStarNode2X>(8);
        }
        public float GetHCost(AStarNode2X start, AStarNode2X end)
        {

            return (float)Math.Sqrt((start.MapPosX - end.MapPosX) * (start.MapPosX - end.MapPosX) + (start.MapPosY - end.MapPosY) * (start.MapPosY - end.MapPosY));
        }

        public AStarNode2X[] GetNeighborNodes(AStarNode2X node)
        {
            neighborNodes.Clear();
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (node.MapPosX - 1 + i >= 0 && node.MapPosX - 1 + i < mapWith)
                    {
                        if (node.MapPosY - 1 + j >= 0 && node.MapPosY - 1 + j < mapHeight)
                        {
                            if (node.MapPosY - 1 + j == node.MapPosY && node.MapPosX - 1 + i == node.MapPosX) continue;
                            if (map[node.MapPosX - 1 + i, node.MapPosY - 1 + j].NodeType == AStarNodeType.Walkable)
                            {
                                neighborNodes.Add(map[node.MapPosX - 1 + i, node.MapPosY - 1 + j]);
                            }
                        }
                    }
                }
            }
            return neighborNodes.ToArray();
        }

        public void Reset()
        {
            for (int i = 0; i < mapHeight; i++)
            {
                for (int j = 0; j < mapWith; j++)
                {
                    map[i, j].Reset();
                }
            }
        }
    }
}
